import pygtk
pygtk.require('2.0')
import gtk
import os
import values
import preset
from channel import Channel
import gobject

class StatusIcon:

	def __init__(self):
		self.systray = gtk.StatusIcon()
		self.systray.set_from_stock(gtk.STOCK_GO_UP)
		self.systray.set_tooltip("Equ")

		self.systray.connect("popup-menu", self.right_click_event)
		self.systray.connect("activate", self.showOrNot)

		self.status_bar = gtk.Statusbar()

		self.must_show = True

	def showOrNot(self, widget):
		if self.must_show:
			self.showEcu()
			self.must_show = False
		else:
			self.hideEcu()
			self.must_show = True

	def showEcu(self):
		self.equ = Equ()
		self.equ.addChannels()
		self.equ.addPresets()
		self.equ.startUp()

	def hideEcu(self, widget):
		self.equ.window.hide()

	def main(self):
		gtk.main()

	def show_about_dialog(self, widget):
	        about_dialog = gtk.AboutDialog()

        	about_dialog.set_destroy_with_parent(True)
	        about_dialog.set_name("Equ")
	        about_dialog.set_version("0.0.1")
	        about_dialog.set_authors(["Guido Accardo <gaccardo@gmail.com>"])
        		
	        about_dialog.run()
	        #about_dialog.destroy()

	def right_click_event(self, icon, button, time):
		menu = gtk.Menu()

	        about = gtk.MenuItem("About")
	        quit = gtk.MenuItem("Quit")
		show = gtk.MenuItem("Show")
		hide = gtk.MenuItem("Hide")
        
	        about.connect("activate", self.show_about_dialog)
	        quit.connect("activate", gtk.main_quit)
		show.connect("activate", self.showEcu)
		hide.connect("activate", self.hideEcu)
		
	        menu.append(about)
		menu.append(show)
		menu.append(hide)
	        menu.append(quit)
        
	        menu.show_all()
	        menu.popup(None, None, gtk.status_icon_position_menu, button, time, self.systray)

class Equ:
	def destroy(self, widget, data=None):
		gtk.main_quit()

	def __init__(self):
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.set_title("Equ")
		self.window.set_default_size(850, 240)
		self.window.show()
		self.window.connect("destroy", self.destroy)

		self.table = gtk.Table(rows=3, columns=4, homogeneous=True)
		self.window.add(self.table)

	def addChannels(self):
		self.lista = [
  	       		{"01. 30 Hz" : Channel("01. 31 Hz", 0, 1, 1, 3, self.table)},
          		{"02. 60 Hz" : Channel("02. 63 Hz", 1, 2, 1, 3, self.table)},
         		{"03. 125 Hz" : Channel("03. 125 Hz", 2, 3, 1, 3, self.table)},
         		{"04. 250 Hz" : Channel("04. 250 Hz", 3, 4, 1, 3, self.table)},
         		{"05. 500 Hz" : Channel("05. 500 Hz", 4, 5, 1, 3, self.table)},
         		{"06. 1 kHz" : Channel("06. 1 kHz", 5, 6, 1, 3, self.table)},
         		{"07. 2 kHz" : Channel("07. 2 kHz", 6, 7, 1, 3, self.table)},
                	{"08. 4 kHz" : Channel("08. 4 kHz", 7, 8, 1, 3, self.table)},
      			{"09. 8 kHz" : Channel("09. 8 kHz", 8, 9, 1, 3, self.table)},
			{"10. 16 kHz" : Channel("10. 16 kHz", 9, 10, 1, 3, self.table)}]

	def getActivePreset(self, widget):
		self.setPresets(type=self.presets_list.get_active_text())

	def startUp(self):
		prest = self.getCurrentPreset()

		if prest == "Rock":
			self.presets_list.set_active(1)
		if preset == "Flat":
			self.presets_list.set_active(0)
		if preset == "Jazz":
			self.presets_list.set_active(2)
		if preset == "Techno":
			self.presets_list.set_active(3)
			

	def getCurrentPreset(self):
		p = preset.Preset()
		cur = p.getCurrentPreset()
		self.setPresets(cur)
		return cur

	def loadPresets(self, type):
		p = preset.Preset()
		if str(type) == "Rock":
			val = p.getRock()
			return val
		elif str(type) == "Jazz":
			val = p.getJazz()
			return val
		elif str(type) == "Flat":
			val = p.getFlat()
			return val
		elif str(type) == "Techno":
			val = p.getTechno()
			return val
		elif str(type) == None:
			val = p.getFlat()
			return val

	def setPresets(self, type):
		vals = self.loadPresets(type)
		count = 0

		for music in self.lista:
			object = music.items()[0][1]
			name   = music.items()[0][0]
			object.setVolume(name, vals[count])
			count = count+1
			
	
	def addPresets(self):
		presets_label = gtk.Label("Presets")
		presets_label.show()
		
		self.table.attach(presets_label, 10, 12, 0, 1)

		self.presets_list = gtk.combo_box_new_text()
		self.presets_list.append_text("Flat")
		self.presets_list.append_text("Rock")
		self.presets_list.append_text("Jazz")
		self.presets_list.append_text("Techno")
		self.presets_list.show()
		self.table.attach(self.presets_list, 10, 11, 1, 2)

		button_save = gtk.Button(label="Cargar")
		button_save.connect("clicked", self.getActivePreset)
		button_save.show()
		self.table.attach(button_save, 11, 12, 1, 2)

	def main(self):
		gtk.main()

if __name__ == '__main__':
	x = StatusIcon()
	x.main()
	
